package org.sorbonne.lex.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.component.html.HtmlDataGrid;
import org.sorbonne.lex.model.LemmeMorph;
import org.sorbonne.lex.service.LexService;

public class LexController {
	private Log log = LogFactory.getLog(LexController.class);

	private LexService lexService;

	private String lexeme;

	private String tag;

	private String kind;

	private String number;

	private Integer maxNbWD;

	private List<LemmeMorph> lemmeMorphList;

	private List<String> lexemes;

	private HtmlDataGrid dataGrid;

	private static final String UNDEFINED = "N/A";

	public HtmlDataGrid getDataGrid() {
		return dataGrid;
	}

	public void setDataGrid(HtmlDataGrid dataGrid) {
		this.dataGrid = dataGrid;
	}

	public List<LemmeMorph> getLemmeMorphList() {
		return lemmeMorphList;
	}

	public void setLemmeMorphList(List<LemmeMorph> lemmeMorphList) {
		this.lemmeMorphList = lemmeMorphList;
	}

	public void setLexService(LexService service) {
		this.lexService = service;
	}

	public String findLemmeMorph() {
		log.debug("lexeme=" + lexeme);
		Set<LemmeMorph> result = lexService.findLemmeMorphSet(lexeme);
		lemmeMorphList = new ArrayList<LemmeMorph>(result != null ? result
				: Collections.<LemmeMorph> emptyList());
		return "success";
	}

	public String findLexemes() {
		log.debug("tag=" + tag);
		List<String> criteria = new ArrayList<String>();
		if (!UNDEFINED.equals(kind)) {
			criteria.add(kind);
		}
		if (!UNDEFINED.equals(number)) {
			criteria.add(number);
		}
		Set<String> resultSet = lexService.findLexemes(tag, criteria);
		lexemes = resultSet != null ? new ArrayList<String>(resultSet)
				: Collections.<String> emptyList();
		return "success";
	}

	public List<String> getLexemes() {
		return lexemes;
	}

	public String getLexeme() {
		return lexeme;
	}

	public void setLexeme(String lexeme) {
		this.lexeme = lexeme;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public void setLexemes(List<String> lexemes) {
		this.lexemes = lexemes;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public Integer getMaxNbWD() {
		return maxNbWD;
	}

	public void setMaxNbWD(Integer maxNbWD) {
		this.maxNbWD = maxNbWD;
		dataGrid.setElements(maxNbWD);
	}

}
